/**
 * 
 */
package persistence.DAO.SQL.entities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import metier.Article;
import metier.Bean;
import metier.Media;
import persistence.ConnectionManager;
import persistence.DAO.DAO;
import persistence.DAO.DAOSQL;

/**
 * @author hozakan
 *
 */
public class DAOSQLMedia extends DAOSQL {

	private static DAO instance;
	private static final Logger logger = Logger.getLogger(DAOSQLMedia.class);
		
	public static DAO getInstance() {
		if (instance == null)
			instance = new DAOSQLMedia();
		return instance;
	}
	
	private DAOSQLMedia() {
		try {
			insert = prepareInsert();
			update = prepareUpdate();
			delete = prepareDelete();
			select = prepareSelect();
		    selectAll = prepareSelectAll();	
		} catch (Exception e) {
			logger.error("Erreur lors de la création des PreparedStatement");
		}
	}


	private boolean create(Bean bean)
	{
		boolean retour = false;
		Media media = (Media) bean;
		try {
			ConnectionManager.getInstance().getConnection();
			insert.clearParameters();
			insert.setString(1, media.getTitre());
			insert.setString(2, media.getUrl());
			insert.setString(3, media.getDescription());
			insert.setFloat(4, media.getPoids());
			insert.setString(5, media.getExtension());
			insert.setDate(6, new java.sql.Date(media.getDateEnvoi().getTime()));
			insert.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			logger.error("Erreur lors de la création d'un media");
		} catch (ClassNotFoundException e) {
			logger.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}

	private boolean update(Bean bean) {
		boolean retour = false;
		Media media = (Media) bean;
		try {
			ConnectionManager.getInstance().getConnection();
			update.clearParameters();
			update.setString(1, media.getTitre());
			update.setString(2, media.getUrl());
			update.setString(3, media.getDescription());
			update.setFloat(4, media.getPoids());
			update.setString(4, media.getExtension());
			update.setDate(5, new java.sql.Date(media.getDateEnvoi().getTime()));
			update.setInt(6, media.getId());
			update.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			logger.error("Erreur lors de la modification d'un media");
		} catch (ClassNotFoundException e) {
			logger.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}
	
	
	@Override
	public boolean effacer(Bean bean) {
		boolean retour = false;
		try {
			ConnectionManager.getInstance().getConnection();
			delete.clearParameters();
			delete.setInt(1, bean.getId());
			delete.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			logger.error("Erreur lors de la suppression d'un media");
		} catch (ClassNotFoundException e) {
			logger.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}

	@Override
	public boolean enregistrer(Bean bean) {
		boolean retour = false;
		if (bean.getId() == 0) {
			retour = create(bean);
		} else {
			retour = update(bean);
		}
		return retour;
	}

	@Override
	protected PreparedStatement prepareDelete() throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("DELETE FROM media WHERE idMedia=?");
		return ps;
	}

	@Override
	protected PreparedStatement prepareInsert()  throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("INSERT INTO media " +
				"( titre, url, description, poids,extension,dateEnvoi) VALUES (?, ?, ?, ?, ?,?)");
		return ps;
	}

	@Override
	protected PreparedStatement prepareUpdate()  throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("UPDATE media SET " +
				"titre=?, url=?, description=?, poids=?, extension=?,dateEnvoi=? WHERE idMedia=?");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareSelect()
	 */
	@Override
	protected PreparedStatement prepareSelect() throws SQLException,
			ClassNotFoundException
	{
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("SELECT * FROM media WHERE idMedia=?");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareSelectAll()
	 */
	@Override
	protected PreparedStatement prepareSelectAll() throws SQLException,
			ClassNotFoundException
	{
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("SELECT * FROM media ");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @see persistence.DAO.DAO#getBeans()
	 */
	@Override
	public List<Bean> getBeans()
	{
		ResultSet rs = null;
		List<Bean> beans = new ArrayList<Bean>();
		try {
			ConnectionManager.getInstance().getConnection();
			rs = selectAll.executeQuery();
			while (rs.next()) {
				Media media = new Media();
				media.setId((Integer.parseInt(rs.getString("idMedia"))));
				media.setTitre(rs.getString("titre"));
				media.setUrl(rs.getString("url"));
				media.setDescription(rs.getString("description"));
				media.setPoids(Float.parseFloat(rs.getString("poids")));
				media.setExtension(rs.getString("extension"));
				media.setDateEnvoi(new java.util.Date(rs.getDate("dateEnvoi").getTime()));
				beans.add(media);
			}
		} catch (SQLException e) {
			logger.error("Erreur lors de la préparation du PreparedStatement");
		} catch (ClassNotFoundException e) {
			logger.error("impossible d'obtenir la connexion");
		}
		return beans;
	}

	/*__________________________________________________________*/
	/**
	 * @param id
	 * @return
	 * @see persistence.DAO.DAO#selectionner(int)
	 */
	@Override
	public Bean selectionner(int id)
	{
		Bean bean = null;
		try {
			ConnectionManager.getInstance().getConnection();
			select.clearParameters();
			select.setInt(1, id);
			ResultSet rs = select.executeQuery();
			while (rs.next()) {
				Media media = new Media();
				media.setId((Integer.parseInt(rs.getString("idMedia"))));
				media.setTitre(rs.getString("titre"));
				media.setUrl(rs.getString("url"));
				media.setDescription(rs.getString("description"));
				media.setPoids(Float.parseFloat(rs.getString("poids")));
				media.setExtension(rs.getString("extension"));
				media.setDateEnvoi(new java.util.Date(rs.getDate("dateEnvoi").getTime()));
				bean = media;
			}
		} catch (SQLException e) {
			logger.error("Erreur lors de la préparation du PreparedStatement");
		} catch (ClassNotFoundException e) {
			logger.error("impossible d'obtenir la connexion");
		}
		return bean;
	}
	
}
